package global

import (
	"Im/server/config"
	"Im/server/util/timer"
	"sync"

	"github.com/patrickmn/go-cache"
	"github.com/redis/go-redis/v9"

	"github.com/songzhibin97/gkit/cache/local_cache"
	"github.com/spf13/viper"
	"go.uber.org/zap"
	"golang.org/x/sync/singleflight"
	"gorm.io/gorm"
)

var (
	JLZS_DB                  *gorm.DB
	JLZS_DBList              map[string]*gorm.DB
	JLZS_REDIS               *redis.Client
	JLZS_CACHE               *cache.Cache
	JLZS_CONFIG              config.Server
	JLZS_VP                  *viper.Viper
	JLZS_LOG                 *zap.Logger
	JLZS_Timer               timer.Timer = timer.NewTimerTask()
	JLZS_Concurrency_Control             = &singleflight.Group{}

	BlackCache local_cache.Cache
	lock       sync.RWMutex
)

// GetGlobalDBByDBName 通过名称获取db list中的db
func GetGlobalDBByDBName(dbname string) *gorm.DB {
	lock.RLock()
	defer lock.RUnlock()
	return JLZS_DBList[dbname]
}

// MustGetGlobalDBByDBName 通过名称获取db 如果不存在则panic
func MustGetGlobalDBByDBName(dbname string) *gorm.DB {
	lock.RLock()
	defer lock.RUnlock()
	db, ok := JLZS_DBList[dbname]
	if !ok || db == nil {
		panic("db no init")
	}
	return db
}
